﻿//========================================================================
// *作者：海盗猫  主页：ilplay.com
// *脚本：UIDisiplayStruct
// *描述：UIDisiplayStruct
// *?2024 HDMCode . All rights reserved.
//* ======================================================================
using System.Collections.Generic;
using UnityEngine;

namespace HDMCode.UIFrame
{
    public class UIEntity:UIEntityBase,UIEntityRelease,IUIEntity
    {
        /// <summary>
        /// 首次加载UI
        /// </summary>
        /// <param name="name">UI名称</param>
        public UIEntity(string name)
        {
            if (HDMUI.Behavior.PreLoad(name,ref UIDisplay))
            {
                UIName = name;
                HDMUI.UI.OnRelease.AddListener(Release);
                m_UiEvent.RemoveComponentsListener(UIDisplay);
                m_UiEvent.AddComponentsListener(this);
            }
            CheckAnimator();
            CheckAudio();
        }

        /// <summary>
        /// 预加载UI资源
        /// </summary>
        public void PreLoad()
        {
            UIDisplay.gameObject.SetActive(false);
        }

        /// <summary>
        /// 获取UI是否正在显示
        /// </summary>
        internal bool IsOn() => IsShow;

        /// <summary>
        /// 显示UI
        /// </summary>
        public void ShowUI()
        {
            if (!IsShow)
            {
                HDMUI.Behavior.DisplayUI(ref UIDisplay);
                if (isFrist)
                    UIDisplay.uiLife?.OnLoadUI(this);
                UIDisplay.uiLife?.OnDisplay(this);
                IsShow = true;
            }
            isFrist = false;
        }

        /// <summary>
        /// 带参打开窗口
        /// </summary>
        /// <param name="parameter">需要携带的参数</param>
        /// <typeparam name="T">任意传递类型</typeparam>
        public void ShowUI<T>(T parameter)
        {
            if (!IsShow)
            {
                HDMUI.Behavior.DisplayUI(ref UIDisplay);
                if (isFrist)
                    UIDisplay.uiLife?.OnLoadUI<T>(parameter,this);
                UIDisplay.uiLife?.OnDisplay(parameter,this);
                IsShow = true;
            }
            isFrist = false;
        }

        /// <summary>
        /// 克隆显示一个窗口，仅支持通知类型弹窗克隆
        /// </summary>
        public void CloneShow<T>(T parameter)
        {
            if (IsShow && UIDisplay.wrapperType== WrapperType.inform)
            {
                var clone = Object.Instantiate(UIDisplay);
                m_TampCloneUIs.Add(clone);
                HDMUI.Behavior.DisplayUI(ref clone);
                UIDisplay.uiLife?.OnClone(parameter,clone.gameObject,clone.components);
            }
            else
            {
                Debug.LogWarning($"<UIFrame UI对象> 日志：尝试克隆一个没有显示基本UI或者非通知类型的UI！ {UIDisplay.name}");
            }
        }

        /// <summary>
        /// 销毁UI
        /// </summary>
        public void DestroyUI()=>UIDisplay.uiLife?.OnDestroyUI(this,this);
        
        /// <summary>
        /// 播放UI动画
        /// </summary>
        /// <param name="animationName">需要播放的动画名称</param>
        /// <param name="time">需要延时的时间</param>
        public UIEntityEvent Play(string animationName,float time)
        {
            if (m_Animator)
                m_Animator.Play(animationName);
            return AsyncEvent(time);
        }

        /// <summary>
        /// 播放打开音频
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public UIEntityEvent AudioOpen(float time=0.3f)
        {
            if (m_AudioSource.clip)
            {
                m_AudioSource.clip = UIDisplay.openAudio;
                m_AudioSource.Play();
            }
            return AsyncEvent(time);
        }
        
        /// <summary>
        /// 播放关闭音频
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public UIEntityEvent AudioClose(float time=0.3f)
        {
            if (m_AudioSource.clip)
            {
                m_AudioSource.clip = UIDisplay.closeAudio;
                m_AudioSource.Play();
            }
            return AsyncEvent(time);
        }

        /// <summary>
        /// 移除UI
        /// </summary>
        public void Remove()
        {
            RemoveClone();
            HDMUI.Behavior.RemoveUI(UIDisplay);
            IsShow = false;
        }
        
        /// <summary>
        /// 释放UI
        /// </summary>
        public void Release()
        {
            HDMUI.UI.OnRelease.RemoveListener(Release);
            IsShow = false;
            RemoveClone();
            HDMUI.Behavior.ReleaseUIAeeets(UIDisplay);
            HDMUI.UI.RemoveToCache(UIName);
        }

        /// <summary>
        /// 移除克隆体
        /// </summary>
        protected void RemoveClone()
        {
            foreach (var cloneItem in m_TampCloneUIs)
                Object.Destroy(cloneItem.gameObject);
            m_TampCloneUIs = new List<UIDisplay>();
        }
    }
}
